package schema

import (
	"time"

	"lianzi_gos/pkg/util"
)

// 联系我们
type Connection struct {
	ID          string    `json:"id" gorm:"size:20;primarykey;"` // Unique ID
	UID         string    `json:"uid" gorm:"size:32;index"`      // 用户ID
	Username    string    `json:"username" gorm:"size:20;index"` // 联系人姓名
	Phone       string    `json:"phone" gorm:"size:20;index"`    // 联系人手机号
	Types       int64     `json:"types" gorm:"index;default:1;comment:'1 提交 2 已回复'"`
	Description string    `json:"description" gorm:"size:1024"` // Details about dictionary
	Sequence    int       `json:"sequence" gorm:"index;"`       // Sequence for sorting
	Status      string    `json:"status" gorm:"size:20;index"`  // Status of dictionary (disabled, enabled)
	CreatedAt   time.Time `json:"created_at" gorm:"index;"`     // Create time
	UpdatedAt   time.Time `json:"updated_at" gorm:"index;"`     // Update time
}

// Defining the query parameters for the `Connection` struct.
type ConnectionQueryParam struct {
	util.PaginationParam

	LikeName  string `form:"username"` // 联系人姓名
	LikePhone string `form:"phone"`    // 联系人手机号
	Status    string `form:"-"`        // Status of dictionary (disabled, enabled)
}

// Defining the query options for the `Connection` struct.
type ConnectionQueryOptions struct {
	util.QueryOptions
}

// Defining the query result for the `Connection` struct.
type ConnectionQueryResult struct {
	Data       Connections
	PageResult *util.PaginationResult
}

// Defining the slice of `Connection` struct.
type Connections []*Connection

func (a Connections) Len() int {
	return len(a)
}

func (a Connections) Less(i, j int) bool {
	if a[i].Sequence == a[j].Sequence {
		return a[i].CreatedAt.Unix() > a[j].CreatedAt.Unix()
	}
	return a[i].Sequence > a[j].Sequence
}

func (a Connections) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

// Defining the data structure for creating a `Connection` struct.
type ConnectionForm struct {
	Username    string `json:"username" binding:"required,max=20"` // 联系人姓名
	Phone       string `json:"phone" binding:"required,max=20"`    // 联系人手机号
	Description string `json:"description"`                        // Details about dictionary
	UID         string `json:"uid"`                                // Sequence for sorting
	//Status      string `json:"status" binding:"required,oneof=disabled enabled"` // Status of dictionary (disabled, enabled)
}

// A validation function for the `ConnectionForm` struct.
func (a *ConnectionForm) Validate() error {
	return nil
}

// Convert `ConnectionForm` to `Connection` object.
func (a *ConnectionForm) FillTo(connection *Connection) error {
	connection.Username = a.Username
	connection.Phone = a.Phone
	connection.Description = a.Description
	connection.Status = "enabled"
	return nil
}
